package thread.thread_state;

import java.util.concurrent.TimeUnit;

public class ThreadState {

    public static void main(String[] args) {
        new Thread(new WaitingThread(), "WaitingThread").start();
        new Thread(new TimeWaitingThread(), "TimeWaitingThread").start();
        // 使用两个Blocked线程，一个获取锁成功，另一个被阻塞
        new Thread(new BlockedThread(), "BlockedThread-1").start();
        new Thread(new BlockedThread(), "BlockedThread-2").start();
    }

    /**
     * 等待
     */
    static class WaitingThread implements Runnable {

        @Override
        public void run() {
            while (true) {
                synchronized (WaitingThread.class) {
                    try {
                        WaitingThread.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 超时等待
     */
    static class TimeWaitingThread implements Runnable {

        @Override
        public void run() {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 阻塞
     */
    static class BlockedThread implements Runnable {

        @Override
        public void run() {
            synchronized (BlockedThread.class) {
                while (true) {
                    try {
                        TimeUnit.SECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
